/*
 * MeView.java
 *
 * Created on 19 décembre 2007, 10:11
 */
package ourpkr.view;

import java.awt.Dimension;
import java.beans.*;
import java.util.Vector;
import ourpkr.OurPkrApp;
import ourpkr.model.Carte;
import ourpkr.model.EnsembleCartes;

/**
 *
 * @author  niboucha
 */
public class Joueur extends javax.swing.JPanel {

    /** Creates new form MeView */
    int joueurNb;

    public Joueur() {

    }

    public Joueur(int num) {
        initComponents();
        myInits();
        initCarts();
        joueurNb = num;
        this.nomMoi.setText("");
        this.scoreLbl.setText("");
        this.miseLbl.setText("");
        lock();
        initListeners();
        this.nomMoi.setText(OurPkrApp.getApplication().getPc().getJoueur(joueurNb).getNom());
    }

    private void initCarts() {
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ourpkr.OurPkrApp.class).getContext().getResourceMap(Joueur.class);
        carteFactori fact = new carteFactori(Joueur.class.getResource("/" + resourceMap.getResourcesDir() + resourceMap.getString("carteFile")).toString());
        cartes = fact.fact(2);
        Dimension d = new Dimension((int) cartes.elementAt(0).getAW() * 2, (int) cartes.elementAt(0).getAH());
        cartesPanel.setMaximumSize(d);
        cartesPanel.setMinimumSize(d);
        cartesPanel.setPreferredSize(d);
        cartesPanel.setSize(d);
        for (aCarte c : cartes) {
            c.setVisible(false);
            cartesPanel.add(c);
        }
    }

    private void initListeners() {

        OurPkrApp.getApplication().getPc().getJoueur(joueurNb).pcs.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent arg0) {
                if (arg0.getPropertyName().equals("main")) {
                    ((EnsembleCartes) arg0.getNewValue()).pcs.addPropertyChangeListener("cartes", new PropertyChangeListener() {

                        public void propertyChange(PropertyChangeEvent arg0) {
                            Vector<Carte> c = (Vector<Carte>) (arg0.getNewValue());
                            int i = 0;
                            for (; i < c.size() && i < 2; i++) {
                                cartes.elementAt(i).setColor(c.elementAt(i).getCouleur() - 1);
                                cartes.elementAt(i).setVal(c.elementAt(i).getValeur() - 1);
                                cartes.elementAt(i).setVisible(true);
                            }
                            for (; i < 2; i++) {
                                cartes.elementAt(i).setVisible(false);
                            }
                        }
                    });
                } else if (arg0.getPropertyName().equals("fortune")) {
                    scoreLbl.setText(arg0.getNewValue().toString());
                } else if (arg0.getPropertyName().equals("miseCourante")) {
                    miseLbl.setText(arg0.getNewValue().toString());
                } else if (arg0.getPropertyName().equals("dealer")) {
                    dealer.setVisible((new Boolean(arg0.getNewValue().toString())).booleanValue());
                } else if (arg0.getPropertyName().equals("playing")) {
                    playing.setVisible((new Boolean(arg0.getNewValue().toString())).booleanValue());
                } else if (arg0.getPropertyName().equals("looser")) {
                    statutLbl.setText("Plus dans la partie");
                } else if (arg0.getPropertyName().equals("whatToDo")) {
                    int last = new Integer(arg0.getNewValue().toString()).intValue();
                    if (last == -1) {
                        lastCoup.setText("Fold");
                    } else if (last == 0) {
                        lastCoup.setText("Check");
                    } else {
                        lastCoup.setText("Raise to " + last);
                    }
                } else if (arg0.getPropertyName().equals("alwaysInTour")) {
                    if ((new Boolean(arg0.getNewValue().toString())).booleanValue()) {
                        statutLbl.setText("Dans la partie");
                    } else {
                        statutLbl.setText("Couche");
                    }
                }
            }
        });
    }

    private void myInits() {
        dealer.setVisible(false);
        playing.setVisible(false);

    //Creation de l'affichage des cartes
    //org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ourpkr.OurPkrApp.class).getContext().getResourceMap(Joueur.class);
    //setURI(Joueur.class.getResource("/"+resourceMap.getResourcesDir()+resourceMap.getString("carteFile")).toString());
    //cartePanel.add(svg);
    }

    private void updateStatus() {
        ourpkr.controler.PartieComplete pc = OurPkrApp.getApplication().getPc();
        if (pc.getJoueursFinis().contains(pc.getJoueur(joueurNb))) {
            this.statutLbl.setText("Plus dans la partie");
        } else {
            if (pc.getJoueur(joueurNb).isAlwaysInTour()) {
                this.statutLbl.setText("Dans le tour");
            } else {
                this.statutLbl.setText("Couche");
            }
        }
    }

    public void lock() {
        miseTextField.setEnabled(false);
        coucheBt.setEnabled(false);
        miserBt.setEnabled(false);
        suivreBt.setEnabled(false);
    }

    public void unlock() {
        miseTextField.setEnabled(true);
        coucheBt.setEnabled(true);
        miserBt.setEnabled(true);
        suivreBt.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titlePanel = new javax.swing.JPanel();
        nomMoi = new javax.swing.JLabel();
        scorePanel = new javax.swing.JPanel();
        miseNameLbl = new javax.swing.JLabel();
        miseLbl = new javax.swing.JLabel();
        scoreNameLbl = new javax.swing.JLabel();
        scoreLbl = new javax.swing.JLabel();
        cartesPanel = new javax.swing.JPanel();
        playPanel = new javax.swing.JPanel();
        miseTextField = new javax.swing.JTextField();
        coucheBt = new javax.swing.JButton();
        suivreBt = new javax.swing.JButton();
        miserBt = new javax.swing.JButton();
        statusPanel = new javax.swing.JPanel();
        statutNameLbl = new javax.swing.JLabel();
        statutLbl = new javax.swing.JLabel();
        dealer = new javax.swing.JLabel();
        playing = new javax.swing.JLabel();
        lastCoupLeg = new javax.swing.JLabel();
        lastCoup = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        setMinimumSize(new java.awt.Dimension(219, 62));
        setName("Form"); // NOI18N
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ourpkr.OurPkrApp.class).getContext().getResourceMap(Joueur.class);
        titlePanel.setBackground(resourceMap.getColor("titlePanel.background")); // NOI18N
        titlePanel.setMaximumSize(new java.awt.Dimension(369, 42));
        titlePanel.setMinimumSize(new java.awt.Dimension(369, 42));
        titlePanel.setName("titlePanel"); // NOI18N

        nomMoi.setText(resourceMap.getString("nomMoi.text")); // NOI18N
        nomMoi.setName("nomMoi"); // NOI18N
        titlePanel.add(nomMoi);

        add(titlePanel);

        scorePanel.setMaximumSize(new java.awt.Dimension(322, 40));
        scorePanel.setMinimumSize(new java.awt.Dimension(322, 40));
        scorePanel.setName("scorePanel"); // NOI18N

        miseNameLbl.setLabelFor(miseLbl);
        miseNameLbl.setText(resourceMap.getString("miseNameLbl.text")); // NOI18N
        miseNameLbl.setName("miseNameLbl"); // NOI18N

        miseLbl.setText(resourceMap.getString("miseLbl.text")); // NOI18N
        miseLbl.setName("miseLbl"); // NOI18N

        scoreNameLbl.setLabelFor(scoreLbl);
        scoreNameLbl.setText(resourceMap.getString("scoreNameLbl.text")); // NOI18N
        scoreNameLbl.setName("scoreNameLbl"); // NOI18N

        scoreLbl.setText(resourceMap.getString("scoreLbl.text")); // NOI18N
        scoreLbl.setName("scoreLbl"); // NOI18N

        javax.swing.GroupLayout scorePanelLayout = new javax.swing.GroupLayout(scorePanel);
        scorePanel.setLayout(scorePanelLayout);
        scorePanelLayout.setHorizontalGroup(
            scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scorePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scoreNameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scoreLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(miseNameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(miseLbl)
                .addContainerGap(45, Short.MAX_VALUE))
        );
        scorePanelLayout.setVerticalGroup(
            scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scorePanelLayout.createSequentialGroup()
                .addGroup(scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(scoreNameLbl)
                    .addComponent(scoreLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(miseNameLbl)
                    .addComponent(miseLbl))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        add(scorePanel);

        cartesPanel.setName("cartesPanel"); // NOI18N
        cartesPanel.setLayout(new javax.swing.BoxLayout(cartesPanel, javax.swing.BoxLayout.LINE_AXIS));
        add(cartesPanel);

        playPanel.setName("playPanel"); // NOI18N

        miseTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        miseTextField.setText(resourceMap.getString("miseTextField.text")); // NOI18N
        miseTextField.setName("miseTextField"); // NOI18N
        miseTextField.setPreferredSize(new java.awt.Dimension(70, 24));
        playPanel.add(miseTextField);

        coucheBt.setText(resourceMap.getString("coucheBt.text")); // NOI18N
        coucheBt.setName("coucheBt"); // NOI18N
        coucheBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coucheBtActionPerformed(evt);
            }
        });
        playPanel.add(coucheBt);

        suivreBt.setText(resourceMap.getString("suivreBt.text")); // NOI18N
        suivreBt.setName("suivreBt"); // NOI18N
        suivreBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                suivreBtActionPerformed(evt);
            }
        });
        playPanel.add(suivreBt);

        miserBt.setText(resourceMap.getString("miserBt.text")); // NOI18N
        miserBt.setName("miserBt"); // NOI18N
        miserBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miserBtActionPerformed(evt);
            }
        });
        playPanel.add(miserBt);

        add(playPanel);

        statusPanel.setName("statusPanel"); // NOI18N

        statutNameLbl.setLabelFor(statutLbl);
        statutNameLbl.setText(resourceMap.getString("statutNameLbl.text")); // NOI18N
        statutNameLbl.setName("statutNameLbl"); // NOI18N

        statutLbl.setText(resourceMap.getString("statutLbl.text")); // NOI18N
        statutLbl.setName("statutLbl"); // NOI18N

        dealer.setIcon(resourceMap.getIcon("dealer.icon")); // NOI18N
        dealer.setText(resourceMap.getString("dealer.text")); // NOI18N
        dealer.setName("dealer"); // NOI18N

        playing.setIcon(resourceMap.getIcon("playing.icon")); // NOI18N
        playing.setText(resourceMap.getString("playing.text")); // NOI18N
        playing.setName("playing"); // NOI18N

        lastCoupLeg.setText(resourceMap.getString("lastCoupLeg.text")); // NOI18N
        lastCoupLeg.setName("lastCoupLeg"); // NOI18N

        lastCoup.setText(resourceMap.getString("lastCoup.text")); // NOI18N
        lastCoup.setName("lastCoup"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addComponent(statutNameLbl)
                        .addGap(18, 18, 18)
                        .addComponent(statutLbl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                        .addComponent(playing)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dealer))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addComponent(lastCoupLeg)
                        .addGap(18, 18, 18)
                        .addComponent(lastCoup)
                        .addContainerGap(222, Short.MAX_VALUE))))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastCoupLeg)
                    .addComponent(lastCoup))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(statutLbl)
                            .addComponent(statutNameLbl))
                        .addContainerGap())
                    .addComponent(dealer, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(playing, javax.swing.GroupLayout.Alignment.TRAILING)))
        );

        add(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    private void coucheBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coucheBtActionPerformed
        OurPkrApp.getApplication().getPc().getHuman().playAction("fold", 0);
    }//GEN-LAST:event_coucheBtActionPerformed

    private void suivreBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suivreBtActionPerformed
        OurPkrApp.getApplication().getPc().getHuman().playAction("check", 0);
    }//GEN-LAST:event_suivreBtActionPerformed

    private void miserBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miserBtActionPerformed
        try {
            int mise = new Integer(this.miseTextField.getText()).intValue();
            OurPkrApp.getApplication().getPc().getHuman().playAction("raise", mise);
        } catch (Exception e) {
        //Illegal value
        }
    }//GEN-LAST:event_miserBtActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cartesPanel;
    private javax.swing.JButton coucheBt;
    private javax.swing.JLabel dealer;
    private javax.swing.JLabel lastCoup;
    private javax.swing.JLabel lastCoupLeg;
    private javax.swing.JLabel miseLbl;
    private javax.swing.JLabel miseNameLbl;
    private javax.swing.JTextField miseTextField;
    private javax.swing.JButton miserBt;
    private javax.swing.JLabel nomMoi;
    private javax.swing.JPanel playPanel;
    private javax.swing.JLabel playing;
    private javax.swing.JLabel scoreLbl;
    private javax.swing.JLabel scoreNameLbl;
    private javax.swing.JPanel scorePanel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JLabel statutLbl;
    private javax.swing.JLabel statutNameLbl;
    private javax.swing.JButton suivreBt;
    private javax.swing.JPanel titlePanel;
    // End of variables declaration//GEN-END:variables
    protected javax.swing.JPanel getPlayPanel() {
        return playPanel;
    }

    protected javax.swing.JPanel getCartePanel() {
        return cartesPanel;
    }
    Vector<aCarte> cartes;
}
